package org.jeecg.modules.ws.controller;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.ws.entity.WsBondFile;
import org.jeecg.modules.ws.entity.WsCapitalDetail;
import org.jeecg.modules.ws.service.IWsBondFileService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 债卷附件
 * @Author: jeecg-boot
 * @Date:   2025-03-28
 * @Version: V1.0
 */
@Slf4j
@Api(tags="债卷附件")
@RestController
@RequestMapping("/wsBondFile")
public class WsBondFileController extends JeecgController<WsBondFile, IWsBondFileService> {
	@Autowired
	private IWsBondFileService wsBondFileService;
	 @Value(value = "${jeecg.path.upload}")
	 private String uploadpath;

	/**
	 * 分页列表查询
	 *
	 * @param wsBondFile
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "债卷附件-分页列表查询")
	@ApiOperation(value="债卷附件-分页列表查询", notes="债卷附件-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(WsBondFile wsBondFile,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<WsBondFile> queryWrapper = QueryGenerator.initQueryWrapper(wsBondFile, req.getParameterMap());
		Page<WsBondFile> page = new Page<WsBondFile>(pageNo, pageSize);
		IPage<WsBondFile> pageList = wsBondFileService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 添加
	 *
	 * @param wsBondFile
	 * @return
	 */
	@AutoLog(value = "债卷附件-添加")
	@ApiOperation(value="债卷附件-添加", notes="债卷附件-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody WsBondFile wsBondFile) {
		wsBondFileService.save(wsBondFile);
		return Result.OK("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param wsBondFile
	 * @return
	 */
	@AutoLog(value = "债卷附件-编辑")
	@ApiOperation(value="债卷附件-编辑", notes="债卷附件-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody WsBondFile wsBondFile) {
		wsBondFileService.updateById(wsBondFile);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "债卷附件-通过id删除")
	@ApiOperation(value="债卷附件-通过id删除", notes="债卷附件-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		wsBondFileService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "债卷附件-批量删除")
	@ApiOperation(value="债卷附件-批量删除", notes="债卷附件-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.wsBondFileService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "债卷附件-通过id查询")
	@ApiOperation(value="债卷附件-通过id查询", notes="债卷附件-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		WsBondFile wsBondFile = wsBondFileService.getById(id);
		return Result.OK(wsBondFile);
	}

	 /**
	  * 通过bond_id查询
	  *
	  * @param bondId
	  * @return
	  */
	 @AutoLog(value = "债卷附件-通过bond_id查询")
	 @ApiOperation(value="债卷附件-通过bond_id查询", notes="债卷附件-通过bond_id查询")
	 @GetMapping(value = "/queryByBondId")
	 public Result<?> queryByBondId(@RequestParam(name="bondId",required=true) String bondId) {
		 List<WsBondFile> bondFileList = wsBondFileService.queryByBondId(bondId);
		 return Result.OK(bondFileList);
	 }

  /**
   * 导出excel
   *
   * @param request
   * @param wsBondFile
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, WsBondFile wsBondFile) {
      return super.exportXls(request, wsBondFile, WsBondFile.class, "债卷附件");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, WsBondFile.class);
  }

	 /**
	  * 文件上传
	  * @param mf
	  * @return
	  */
	 @PostMapping(value = "/upload")
	 public Result<?> upload(MultipartFile mf,HttpServletRequest request) {
		 WsBondFile bonFile = new WsBondFile();
		 String fileTypeName = request.getHeader("fileTypeName");
		 String uploadBy = request.getHeader("uploadBy");
		 String bondId = request.getHeader("bondId");
		 try {
			 File file = new File(uploadpath + "/" + fileTypeName + File.separator);
			 if (!file.exists()) {
				 file.mkdirs();// 创建文件根目录
			 }
			 String fileName = mf.getOriginalFilename();
			 long size = mf.getSize();
			 String savePath = file.getPath() + File.separator + fileName;
			 File savefile = new File(savePath);
			 FileCopyUtils.copy(mf.getBytes(), savefile);
			 String dbpath = savePath;
			 if (dbpath.contains("\\")) {
				 dbpath = dbpath.replace("\\", "/");
			 }
			 bonFile.setFile(dbpath);
			 bonFile.setUploadBy(uploadBy);
			 bonFile.setBondId(bondId);
			 bonFile.setUploadTime(new Date());
			 bonFile.setFileSize(BigDecimal.valueOf(size));
			 wsBondFileService.save(bonFile);
			 return Result.OK(dbpath);
		 } catch (IOException e) {
			 log.error(e.getMessage(), e);
			 return Result.error("文件上传失败");
		 }
	 }
}
